package br.unisinos.projetotcc.managedbean;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import br.unisinos.projetotcc.entity.Classe;
import br.unisinos.projetotcc.jarfile.GenerateJar;

@SessionScoped
@ManagedBean(name="uploadMB")
public class UploadMB extends BaseMB<Classe> {

	public UploadMB() {
		setObject(new Classe());
	}
	
	public void handleFileUpload(FileUploadEvent event) throws MalformedURLException, ClassNotFoundException {
		UploadedFile file = event.getFile();
		
		if(getObject().getFile() == null){
			getObject().setFile(new ArrayList<UploadedFile>());
		}
		
		getObject().getFile().add(file);
	}
	
	public void upload() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException{
		  
		
		ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
		
        String jarName = "generateForWeb.jar";
        
		String path = context.getRealPath(File.separator) + jarName;
		
		gerarJar(path);
		
		addClassLoader(path);

		
	}

	protected void addClassLoader(String path) throws MalformedURLException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
		
		
		URL[] classpath = new URL[] { new File(path).toURI().toURL() };

		URLClassLoader urlClassLoader1 = new URLClassLoader(classpath, Thread.currentThread().getContextClassLoader());
		
//		java.lang.ClassLoader cl = new Class
		
		Object invoke = urlClassLoader1.loadClass("Teste").getMethod("main", String[].class).invoke(null, (Object) new String[] { "1", "2" });
		
		/*ByteArrayOutputStream out = new ByteArrayOutputStream();

		PrintStream lastOut = System.out;
		InputStream lastIn = System.in;

		System.setOut(new PrintStream(out));
		System.setIn(new ByteArrayInputStream("1\n2\n".getBytes()));*/

		
		
//		urlClassLoader1.loadClass("org.marloader.teste1.Boba").getMethod("main", String[].class).invoke(null, (Object) new String[] { "1", "2" });

		/*System.setOut(lastOut);
		System.setIn(lastIn);

		System.out.println("RESULTADO:");
		System.out.println(out.toString().trim());*/
		
		
	}

	public void gerarJar(String path) {
		File jar = new File(path);
		new GenerateJar().createJarArchive(jar, getObject().getFile());
	}
	
	public static void main(String[] args) throws IOException {
//		String path1 = "f://" + File.separator+username+File.separator+progName; 
		String path1 = "C:\\Users\\MARVAN\\Desktop\\teste\\Teste.java";


		Runtime runtime = Runtime.getRuntime();
		String command = "javac " + path1;
		System.out.println(command);
		Process process = runtime.exec(command);
		Process process2 = runtime.exec("java Teste");
		
		String[] envp = new String[0];
//		Process process = runtime.exec(command, envp , new File(path1));
		System.out.println(process2.getOutputStream());
		
		
		
		
		
		
		
	}
	
}
